package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @author: 牟治宇
 * @description 商品实现类
 * @date: 2022/7/29 16:09
 */
@Slf4j
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto detailDto = new ProductDetailDto();
            detailDto.setDetail(itemDesc.getItemDesc());
            detailDto.setLimitNum(Long.valueOf(item.getLimitNum()));
            detailDto.setProductId(item.getId());
            detailDto.setProductImageBig(item.getImageBig());
            detailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            detailDto.setProductName(item.getTitle());
            detailDto.setSalePrice(item.getPrice());
            detailDto.setSubTitle(item.getSellPoint());
            response.setProductDetailDto(detailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try {
            request.requestCheck();
            Integer page = request.getPage();
            Integer size = request.getSize();
            PageHelper.startPage(page, size);
            Example example = new Example(Item.class);
            if ("1".equals(request.getSort())) {
                example.setOrderByClause("price");
            } else if ("-1".equals(request.getSort())) {
                example.setOrderByClause("price" + " desc");
            } else {
                example.setOrderByClause("id");
            }

            Example.Criteria criteria = example.createCriteria();
            if (request.getPriceLte() != null) {
                criteria.andLessThanOrEqualTo("price", request.getPriceLte());
            }
            if (request.getPriceGt() != null) {
                criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
            }
            if (request.getCid() != null) {
                criteria.andEqualTo("cid", request.getCid());
            }
            List<Item> items = itemMapper.selectByExample(example);
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            List<ProductDto> productDtos = productConverter.items2Dto(items);
            long total = pageInfo.getTotal();
            response.setTotal(total);
            response.setProductDtoList(productDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }


        return response;
    }


    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();

        try {
            HashSet<PanelDto> panelDtos = new HashSet<>();
            List<Panel> panelList = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            for (int i = 0; i < panelList.size(); i++) {
                Panel panel = panelList.get(i);
                List<PanelContentItem> panelContentItems = panelContentMapper.
                        selectPanelContentAndProductWithPanelId(panel.getId());
                PanelDto panelDto = contentConverter.panel2Dto(panel);
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);

                panelDto.setPanelContentItems(panelContentItemDtos);
                panelDtos.add(panelDto);
            }
            response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllItemResponse getAllItems() {
        AllItemResponse response = new AllItemResponse();

        try {
            List<Item> items = itemMapper.selectAll();

            List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);

            response.setAllItems(itemDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("IProductServiceImpl.getAllItems Occur Exception :" + e);
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
